#!/bin/sh

. "$TEST_SHELL_PATH/abstract-dbops"

RUN_SCRIPT_PATH="$PROJECT_PATH/src/main/resources/templates/run-restart.sh"
PATRONI_CONTAINER_NAME=patroni
SET_RESULT_SCRIPT_PATH=""
OP_NAME=restart
NORMALIZED_OP_NAME='restart'
KEBAB_OP_NAME='restart'
POD_0_RESTARTED=false
POD_1_RESTARTED=false
PATRONI_CONTAINER_NAME=patroni

shell_unit_test_dbops_restart() {
  echo false > "$TEST_TARGET_PATH/pod_0_restarted"
  echo false > "$TEST_TARGET_PATH/pod_1_restarted"
  echo "pod/test-0" > "$TEST_TARGET_PATH/primary_pod"

  shell_unit_test_dbops
}

shell_unit_test_skip() {
  if [ -z "$IMAGE_NAME" ] && echo "$IMAGE_NAME" | grep "patroni"
  then
    return 0;
  else
    return 1;
  fi
}

mock kubectl kubectl_mock

kubectl_mock() {
  mokked
  case "$*" in
    ("get $CLUSTER_CRD_NAME "*" --template="'
  LOCK_POD='*'
  LOCK_TIMEOUT='*'
  RESOURCE_VERSION={{ .metadata.resourceVersion }}
  ')
    echo '
      LOCK_POD=test
      LOCK_TIMEOUT='"$(( $(date +%s) + 1 ))"'
      RESOURCE_VERSION=test
      '
    ;;
    ("annotate $CLUSTER_CRD_NAME "*)
    ;;
    ("get $CLUSTER_CRD_NAME.$CRD_GROUP -n $CLUSTER_NAMESPACE $CLUSTER_NAME --template={{ if .status.dbOps }}{{ if .status.dbOps.$OP_NAME }}true{{ end }}{{ end }}")
    ;;
    ("get $DBOPS_CRD_NAME -n $CLUSTER_NAMESPACE $DBOPS_NAME --template={{ if .status }}true{{ else }}false{{ end }}")
    printf '%s' false
    ;;
    ("get $DBOPS_CRD_NAME -n $CLUSTER_NAMESPACE $DBOPS_NAME --template={{ if .status.restart }}replace{{ else }}add{{ end }}")
    printf '%s' add
    ;;
    ("get $CLUSTER_CRD_NAME.$CRD_GROUP -n $CLUSTER_NAMESPACE $CLUSTER_NAME -o json")
      echo '{
          "apiVersion": "stackgres.io/v1",
          "kind": "SGCluster",
          "metadata": {
              "name": "'"$CLUSTER_NAME"'",
              "namespace": "'"$CLUSTER_NAMESPACE"'"
          },
          "spec": {
              "instances": 2,
              "postgres": {
                "version": "12.6"
              },
              "sgInstanceProfile": "size-s"
          },
          "status": {
            "dbOps": {
              "restart": {
                "initialInstances": [
                    "'"$CLUSTER_NAME-0"'",
                    "'"$CLUSTER_NAME-1"'"
                ]
              }
            }
          }
        }'
    ;;
    ("replace --raw /apis/$CRD_GROUP/v1/namespaces/$CLUSTER_NAMESPACE/$CLUSTER_CRD_NAME/$CLUSTER_NAME -f"*)

    ;;
    ("get pods -n $CLUSTER_NAMESPACE -l $CLUSTER_POD_LABELS -o name")
    echo "pod/test-0"
    echo "pod/test-1"
    ;;
    ("get pods -n $CLUSTER_NAMESPACE -l $CLUSTER_PRIMARY_POD_LABELS -o name")
    cat "$TEST_TARGET_PATH/primary_pod"
    ;;
    ("get pod -n $CLUSTER_NAMESPACE test-0 -o name")
    echo "pod/test-0"
    ;;
    ("get pod -n $CLUSTER_NAMESPACE test-1 -o name")
    echo "pod/test-1"
    ;;
    ("get pod -n $CLUSTER_NAMESPACE test-2 -o name")
    echo "pod/test-2"
    ;;
    ("get $DBOPS_CRD_NAME -n $CLUSTER_NAMESPACE $DBOPS_NAME -o json")
      echo '{
        "apiVersion": "stackgres.io/v1",
        "kind": "SGDbOps",
        "metadata": {
          "name": "'"$DBOPS_NAME"'",
          "namespace": "'"$CLUSTER_NAMESPACE"'",
          "resourceVersion": "1",
          "uid": "172381f8-3f37-46c9-9a29-8284a73d1e5e"
        },
        "spec": {
          "sgCluster": "'"$CLUSTER_NAME"'",
          "op": "restart"
        }
      }'
    ;;
    ("create --raw /api/v1/namespaces/$CLUSTER_NAMESPACE/events -f"*)
    ;;
    ("get pod -n $CLUSTER_NAMESPACE test-0 --template {{ .metadata.creationTimestamp }}")
    if [ "$(cat "$TEST_TARGET_PATH/pod_0_restarted")" = "true" ]
    then
      echo "$(date_iso8601)"
    else
      echo "2021-06-15T00:00:00Z"
    fi
    ;;
    ("get pod -n $CLUSTER_NAMESPACE test-1 --template {{ .metadata.creationTimestamp }}")
    if [ "$(cat "$TEST_TARGET_PATH/pod_1_restarted")" = "true" ]
    then
      echo "$(date_iso8601)"
    else
      echo "2021-06-15T00:00:00Z"
    fi
    ;;
    ("get $CLUSTER_CRD_NAME -n $CLUSTER_NAMESPACE $CLUSTER_NAME -o=jsonpath={ .status.podStatuses[?(@.name == \"test-0\")].pendingRestart }")
    ;;
    ("get $CLUSTER_CRD_NAME -n $CLUSTER_NAMESPACE $CLUSTER_NAME -o=jsonpath={ .status.podStatuses[?(@.name == \"test-1\")].pendingRestart }")
    ;;
    ("get pod -n $CLUSTER_NAMESPACE test-0 --template={{ .metadata.annotations.status }}")
    echo '"pending_restart":false'
    ;;
    ("get pod -n $CLUSTER_NAMESPACE test-1 --template={{ .metadata.annotations.status }}")
    echo '"pending_restart":false'
    ;;
    ("get pod -n $CLUSTER_NAMESPACE test-0 --template={{ index .metadata.labels \"controller-revision-hash\" }}")
    if [ "$(cat "$TEST_TARGET_PATH/pod_0_restarted")" = "true" ]
    then
      echo hash-1
    else
      echo hash-0
    fi
    ;;
    ("get pod -n $CLUSTER_NAMESPACE test-1 --template={{ index .metadata.labels \"controller-revision-hash\" }}")
    if [ "$(cat "$TEST_TARGET_PATH/pod_1_restarted")" = "true" ]
    then
      echo hash-1
    else
      echo hash-0
    fi
    ;;
    ("get sts -n $CLUSTER_NAMESPACE $CLUSTER_NAME --template={{ .status.updateRevision }}")
    echo "hash-1"
    ;;
    ("patch $CLUSTER_CRD_NAME.$CRD_GROUP -n $CLUSTER_NAMESPACE $CLUSTER_NAME "*)
    ;;
    ("patch $CLUSTER_CRD_NAME -n $CLUSTER_NAMESPACE $CLUSTER_NAME "*)
    ;;
    ("patch $DBOPS_CRD_NAME -n $CLUSTER_NAMESPACE $DBOPS_NAME "*)
    ;;
    ("wait pod -n $CLUSTER_NAMESPACE test-"*" --for condition=Ready --timeout 0")
    ;;
    ("delete pod -n $CLUSTER_NAMESPACE test-0")
    echo true > "$TEST_TARGET_PATH/pod_0_restarted"
    ;;
    ("delete pod -n $CLUSTER_NAMESPACE test-1")
    echo true > "$TEST_TARGET_PATH/pod_1_restarted"
    ;;
    ("exec -n $CLUSTER_NAMESPACE test-0 -c $PATRONI_CONTAINER_NAME -- patronictl switchover "*)
    echo "pod/test-1" > "$TEST_TARGET_PATH/primary_pod"
    ;;
    ("exec -n $CLUSTER_NAMESPACE test-0 -c $PATRONI_CONTAINER_NAME -- patronictl "*)
    ;;
    *)
    not_mokked
    ;;
  esac
}
